//
//  STOrderActionSuccessViewController.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/30.
//

import UIKit

class STOrderActionSuccessViewController: GPBaseViewController {
    
    var orderId: Int = 0
    var status: Int = 0
    var orderType: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        let str = getTitle()
        setNavBarTitle(str)
        addTopBGView()

        let h = 424 * (kScreenWidth - 32) / 343
        let bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        bgView.layer.cornerRadius = 12
        view.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(24)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(h)
        }
        
        let imgView: UIImageView = UIImageView.init(image: UIImage.init(named: "img_gou"))
        bgView.addSubview(imgView)
        imgView.snp.makeConstraints { (make) in
            make.top.equalTo(42)
            make.centerX.equalTo(bgView)
            make.width.equalTo(74)
            make.height.equalTo(74)
        }
        
        let lb = UILabel.init()
        lb.text = "恭喜，".appending(str).appending("！")
        lb.textAlignment = .center
        lb.textColor = UIColor.init(hexString: "#323232", alpha: 1)
        lb.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.heavy)
        bgView.addSubview(lb)
        lb.snp.makeConstraints { (make) in
            make.top.equalTo(imgView.snp.bottom).offset(22)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(30)
        }
        
        let btn = UIButton.init(type: .custom)
        btn.setTitle("进入订单状态", for: .normal)
        btn.layer.cornerRadius = 25
        btn.backgroundColor = COLOR_HEX(hexString: "#3686FF", alpha: 1)
        btn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        bgView.addSubview(btn)
        btn.snp.makeConstraints { (make) in
            make.top.equalTo(lb.snp.bottom).offset(126)
            make.left.equalTo(56)
            make.right.equalTo(-56)
            make.height.equalTo(50)
        }
        
        let btn1 = UIButton.init(type: .custom)
        btn1.layer.cornerRadius = 25
        btn1.setTitleColor(COLOR_HEX(hexString: "#8F92A1", alpha: 1), for: UIControl.State.normal)
        btn1.setAttributedTitle(NSAttributedString.init(string: "返回首页", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.heavy)]), for: UIControl.State.normal)
        bgView.addSubview(btn1)
        btn1.snp.makeConstraints { (make) in
            make.top.equalTo(btn.snp.bottom).offset(14)
            make.centerX.equalTo(bgView)
            make.width.equalTo(74)
            make.height.equalTo(25)
        }
        
        btn.addTarget(self, action: #selector(enterOrderStatePage), for: UIControl.Event.touchUpInside)
        btn1.addTarget(self, action: #selector(backHomePage), for: UIControl.Event.touchUpInside)
    }
    
    func getTitle() -> String {
        if (status == 2) {
            return "接单成功"
        } else if (orderType.equalTo("0")){
            if(status == 3) {
                return "提空箱成功"
            } else if (status == 6) {
                return "装箱成功"
            }  else if (status == 9) {
                return "落重箱成功"
            }
        } else {
            if(status == 3) {
                return "提重箱成功"
            }  else if (status == 6) {
                return "卸货成功"
            }  else if (status == 9) {
                return "还空箱成功"
            }
        }
        return ""
    }
    
    @objc func enterOrderStatePage() {
        let len = navigationController!.viewControllers.count
        if(status != 2) {
            navigationController!.popToViewController(navigationController!.viewControllers[len - 2], animated:true)
        } else {
            let vc: STOrderNewDetailViewController = STOrderNewDetailViewController.init()
            vc.orderId = orderId
            vc.orderType = orderType
            vc.status = status
            navigationController?.pushViewController(vc, animated: true)
        }
    }
   
    @objc func backHomePage() {
        navigationController?.popToRootViewController(animated: true)
    }
    
    override func backBarButtoAction() {
        if (status == 2) {
            navigationController?.popViewController(animated: true)
        } else {
            let viewControllers = navigationController!.viewControllers
            let vc = viewControllers[viewControllers.count - 2]
            navigationController?.popToViewController(vc, animated: true)
        }
    }
}
